package com.example.springbootxscjxt.controller;

import com.example.springbootxscjxt.entity.Student;
import com.example.springbootxscjxt.mapper.StudentMapper;
import com.example.springbootxscjxt.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;
    @GetMapping("/index")
    public Map<String,Object> index(@RequestParam  String name,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        pageNum=(pageNum-1)*pageSize;
        Map<String,Object> res=new HashMap<>();
        List<Student> data=studentMapper.selectxs(name, pageNum,pageSize);
Integer total=studentMapper.totalss(name);

        res.put("code",200);
        res.put("data",data);
        res.put("total",total);
        return  res;


    }
    @GetMapping("/all")
    public Map<String,Object> index1(){

        Map<String,Object> res=new HashMap<>();
        List<Student> data=studentMapper.cx();

        res.put("code",200);
        res.put("data",data);

        return  res;


    }
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody Student student){
Map<String,Object> res=new HashMap<>();
if(studentService.save(student) == 1){
    res.put("code",200);
    res.put("msg","新增成功");
}
return res;
    }
    @PostMapping("/update")
    public  Map<String,Object> update(@RequestBody Student student){
        Map<String,Object> res=new HashMap<>();
        if(studentService.updatee(student) == 1){
            res.put("code",200);
            res.put("msg","修改成功");
        }
        return res;
    }
    @GetMapping("/delete")
    public  Map<String,Object> update(@RequestParam String id){
        Map<String,Object> res=new HashMap<>();
        if(studentMapper.delede(id) == 1){
            res.put("code",200);
            res.put("msg","删除成功");
        }
        return res;
    }
}
